package com.sunther.document.entity;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.util.List;

/**
 * @author csb
 * @description: TODO
 * @date 2023/5/16 10:38
 */
@TableName(value = "document_role_tag_v1")
public class RoleTagEntity extends BaseEntity{

    @TableId(value="id",type = IdType.AUTO)
    private Integer id;

    private Integer parentId;

    private String parentName;

    private String name;

    private String code;

    private Integer sort;

    @TableField(exist = false)
    private boolean ifYes = false;

    @TableField(exist = false)
    private List<RoleTagEntity> children;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getParentName() {
        return parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public boolean isIfYes() {
        return ifYes;
    }

    public void setIfYes(boolean ifYes) {
        this.ifYes = ifYes;
    }

    public List<RoleTagEntity> getChildren() {
        return children;
    }

    public void setChildren(List<RoleTagEntity> children) {
        this.children = children;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    /**
     * hutool 树结构工具
     * @param roleTagList
     * @return
     */
    public static List<Tree<String>> getTrees(List<RoleTagEntity> roleTagList) {
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //默认为id
        treeNodeConfig.setIdKey("id");
        //默认为parentId
        treeNodeConfig.setParentIdKey("parentId");
        //排序字段
        treeNodeConfig.setWeightKey("sort");
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        //2.转为树结构,其中rootId参数代表根节点的父级id值
        List<Tree<String>> treeList = TreeUtil.build(roleTagList, "0", treeNodeConfig,
                (treeNode, tree)-> {
                    //对key进行映射赋值
                    tree.setId(String.valueOf(treeNode.getId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getSort());
                    tree.setName(treeNode.getName());
                    tree.put("ifYes",treeNode.isIfYes());
                });
        return treeList;
    }
}
